package nconv.convert;

import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.converter.AbstractWordConverter;
import org.apache.poi.hwpf.converter.TextDocumentFacade;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.util.XMLHelper;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.ParserConfigurationException;
import java.util.List;

public abstract class BaseWordConverter extends AbstractWordConverter {

	
	protected static final String EMPTY_STRING = "";
	protected static final String LINE_BREAK = "\n";
	protected TextDocumentFacade textDocumentFacade = null;

	//protected WikiPicturesManager picManager;

	public BaseWordConverter() {
		try {
			textDocumentFacade = new TextDocumentFacade(
					XMLHelper.getDocumentBuilderFactory().newDocumentBuilder().newDocument());
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public Document getDocument() {
		return textDocumentFacade.getDocument();
	}
	
  protected void outputCharacters(Element block, CharacterRun characterRun,
      String text) {
  	text = text.replaceAll("IP\u001eCAN", "IP-CAN")
						.replaceAll("IP‑CAN", "IP-CAN")
						.replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "").replace((char)0x00a0, ' ');
		block.appendChild( textDocumentFacade.createText( text));
  }

	@Override
	protected void processBookmarks(HWPFDocumentCore wordDocument, Element currentBlock, Range range,
	    int currentTableLevel, List<Bookmark> rangeBookmarks) {
		processCharacters(wordDocument, currentTableLevel, range, currentBlock);
	}

	@Override
	protected void processDocumentInformation(SummaryInformation summaryInformation) {
	}

	@Override
	protected void processDrawnObject(HWPFDocument doc, CharacterRun characterRun, OfficeDrawing officeDrawing,
	    String path, Element block) {
		block.appendChild(textDocumentFacade.createText(
				"[[File:" + path + "|500px|thumb|center|An office drawing object]]"));
	}

	@Override
	protected void processEndnoteAutonumbered(HWPFDocument wordDocument, int noteIndex, Element block,
	    Range endnoteTextRange) {
	}

	@Override
	protected void processFootnoteAutonumbered(HWPFDocument wordDocument, int noteIndex, Element block,
	    Range footnoteTextRange) {
	}

	@Override
	protected void processHyperlink(HWPFDocumentCore wordDocument, Element currentBlock, Range textRange,
	    int currentTableLevel, String hyperlink) {
	}

	@Override
	protected void processImage(Element currentBlock, boolean inlined, Picture picture, String url) {
		currentBlock.appendChild(textDocumentFacade.createText(
				"[[File:" + url + "|500px|thumb|center|An image object]]"));
	}

	@Override
	protected void processImageWithoutPicturesManager(Element currentBlock, boolean inlined, Picture picture) {
		System.out.println("No picture manager");
	}

	@Override
	protected void processLineBreak(Element block, CharacterRun characterRun) {
	}

	@Override
	protected void processPageBreak(HWPFDocumentCore wordDocument, Element flow) {
	}

	@Override
	protected void processPageref(HWPFDocumentCore wordDocument, Element currentBlock, Range textRange,
	    int currentTableLevel, String pageref) {
	}

	@Override
	protected void processSection(HWPFDocumentCore wordDocument, Section section, int s) {
		Element sectionElement = textDocumentFacade.createBlock();
		processParagraphes(wordDocument, sectionElement, section, Integer.MIN_VALUE);
		sectionElement.appendChild(textDocumentFacade.createText("\n"));
		textDocumentFacade.getBody().appendChild(sectionElement);
	}

	@Override
	protected void processTable(HWPFDocumentCore wordDocument, Element flow, Table table) {
	}
	
	public Element getDocumentHead() {
		return textDocumentFacade.getHead();
	}
	
	public Element getDocumentBody() {
		return textDocumentFacade.getBody();
	}

}
